package com.google.memoryofaplace.controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONObject;
import com.google.memoryofaplace.BLAdmins;
import com.google.memoryofaplace.BLDenounces;
import com.google.memoryofaplace.BLPlaces;
import com.google.memoryofaplace.Codes;
import com.google.memoryofaplace.General;
import com.google.memoryofaplace.model.TBJQueryParams;

public class ADMHandler extends HttpServlet {
	private static final long serialVersionUID = 8410724390309880878L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		// redirect to post
		doPost(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		res.setCharacterEncoding("UTF-8");
		res.setContentType("application/json;charset=UTF-8");
		String action = req.getParameter("action");
		String strValidation = General.validateParams(req, General.ADM_ACTIONS);
		JSONObject jReturn;

		try {
			if (!strValidation.equals("")) {
				res.getWriter().print(
						General.returnError(General
								.getLocaleMessage("invalidParams")));
			} else {
				// ADMINS
				if (action.equals("addAdminUser")) {
					if (Codes.UserType.find(Integer.parseInt(req
							.getParameter("arg1"))) == null) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						if (BLAdmins.existsAdminUser(req.getParameter("arg0"))) {
							jReturn = General.returnError(General
									.getLocaleMessage("duplicateRecord"));
						} else {
							jReturn = BLAdmins.createAdmin(
									req.getParameter("arg0"),
									Integer.parseInt(req.getParameter("arg1")));
						}

					}
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getAdminUsers")) {
					jReturn = BLAdmins.getAdminUsers();
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("updatePlace")) {
					jReturn = BLPlaces.updatePlace(req.getParameter("arg0"),
							req.getParameter("arg1"), req.getParameter("arg2"),
							req.getParameter("arg3"), req.getParameter("arg4"),
							req.getParameter("arg5"), req.getParameter("arg6"),
							req.getParameter("arg7"), req.getParameter("arg8"),
							req.getParameter("arg9"),
							req.getParameter("arg10"),
							req.getParameter("arg11"),
							req.getParameter("arg12"));
					res.getWriter().print(jReturn.toString());
				}

				if (action.equals("getDenounces")) {
					jReturn = BLDenounces.getDenounces();
					res.getWriter().print(jReturn.toString());
				}

				// AJAX
				if (action.equals("getPlaces")) {
					TBJQueryParams params = ValidateParams.getParams(req);
					if (params == null) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						jReturn = BLPlaces.getAjaxPlaces(params);

					}
					res.getWriter().print(jReturn.toString());
					// String strJson =
					// "{\"sEcho\": 1, \"iTotalRecords\": 57, \"iTotalDisplayRecords\": 57, \"aaData\": [ [\"Gecko\",\"Firefox 1.0\",\"Win 98+ / OSX.2+\",\"1.7\",\"A\"],[\"Gecko\",\"Firefox 1.5\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Firefox 2.0\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Firefox 3.0\",\"Win 2k+ / OSX.3+\",\"1.9\",\"A\"],[\"Gecko\",\"Camino 1.0\",\"OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Camino 1.5\",\"OSX.3+\",\"1.8\",\"A\"],[\"Gecko\",\"Netscape 7.2\",\"Win 95+ / Mac OS 8.6-9.2\",\"1.7\",\"A\"],[\"Gecko\",\"Netscape Browser 8\",\"Win 98SE+\",\"1.7\",\"A\"],[\"Gecko\",\"Netscape Navigator 9\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Mozilla 1.0\",\"Win 95+ / OSX.1+\",\"1\",\"A\"]] }";
					// res.getWriter().print(strJson);
				}

				// AJAX
				if (action.equals("getPlaceMedia")) {
					TBJQueryParams params = ValidateParams.getParams(req);
					if (params == null) {
						jReturn = General.returnError(General
								.getLocaleMessage("invalidParams"));
					} else {
						jReturn = BLPlaces.getAjaxPlaceMedia(params);

					}
					res.getWriter().print(jReturn.toString());
					// String strJson =
					// "{\"sEcho\": 1, \"iTotalRecords\": 57, \"iTotalDisplayRecords\": 57, \"aaData\": [ [\"Gecko\",\"Firefox 1.0\",\"Win 98+ / OSX.2+\",\"1.7\",\"A\"],[\"Gecko\",\"Firefox 1.5\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Firefox 2.0\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Firefox 3.0\",\"Win 2k+ / OSX.3+\",\"1.9\",\"A\"],[\"Gecko\",\"Camino 1.0\",\"OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Camino 1.5\",\"OSX.3+\",\"1.8\",\"A\"],[\"Gecko\",\"Netscape 7.2\",\"Win 95+ / Mac OS 8.6-9.2\",\"1.7\",\"A\"],[\"Gecko\",\"Netscape Browser 8\",\"Win 98SE+\",\"1.7\",\"A\"],[\"Gecko\",\"Netscape Navigator 9\",\"Win 98+ / OSX.2+\",\"1.8\",\"A\"],[\"Gecko\",\"Mozilla 1.0\",\"Win 95+ / OSX.1+\",\"1\",\"A\"]] }";
					// res.getWriter().print(strJson);
				}

				// AJAX
				if (action.equals("getPlace")) {
					jReturn = BLPlaces.getPlaceExtended(Long.parseLong(req
							.getParameter("arg0")));
					res.getWriter().print(jReturn.toString());
				}

			}
		} catch (IllegalArgumentException e) {
			res.getWriter().print(
					General.returnError(General
							.getLocaleMessage("invalidParams")));
		} catch (Exception e) {
			res.getWriter().print(
					General.returnError("error:" + e.getMessage()));
		}

	}
}
